<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>斐波拉及数列</title>
  </head>
  <body>
    <script>
      /*递归实现*/
      function fib1(n) {
        if (n <= 1) return n;
        else return fib1(n - 1) + fib1(n - 2);
      }
      /*动态规划*/
      function fib2(n) {
        if (n <= 1) return n;
        let p = 0, //p:前前一项
          q = 1, //q：前一项
          result; //当前结果
        for (let i = 2; i <= n; i++) {
          result = p + q; //当前结果：前一项加前前一项
          p = q;
          q = result;
        }
        return result;
      }
    </script>
  </body>
</html>
